package com.yjhb.android.sbz.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.manager.ResManager;


public
class SmallBallView extends View {

    public SmallBallView(Context context) {
        this(context, null);
    }

    public SmallBallView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SmallBallView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SmallBallView);
        ballColor = typedArray.getColor(R.styleable.SmallBallView_ballColor, Color.RED);
        lineColor = typedArray.getColor(R.styleable.SmallBallView_lineColor, Color.GRAY);
        init();
    }

    private Paint paint;
    private int ballColor;
    private int lineColor;
    private boolean isShowTopLine = true;
    private boolean isShowBottomLine = true;

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBall(canvas);
        drawLine(canvas);
    }

    private void drawBall(Canvas canvas) {
        paint.setColor(ballColor);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, paint);
    }

    private void drawLine(Canvas canvas) {
        float y = getHeight() / 2 - getWidth() / 2;
        paint.setColor(lineColor);
        if (isShowTopLine) {
            canvas.drawLine(getWidth() / 2, y - ResManager.dp()*5, getWidth() / 2, 0, paint);
        }
        if (isShowBottomLine) {
            canvas.save();
            canvas.rotate(180, getWidth() / 2, getHeight() / 2);
            canvas.drawLine(getWidth() / 2, y - ResManager.dp()*5, getWidth() / 2, 0, paint);
            canvas.restore();
        }
    }


    public void setBallColor(int color) {
        this.ballColor = color;
    }

    public void setLineColor(int color) {
        this.lineColor = color;
    }

    public void setShowTopLine(boolean showTopLine) {
        isShowTopLine = showTopLine;
    }

    public void setShowBottomLine(boolean showBottomLine) {
        isShowBottomLine = showBottomLine;
    }
}
